<template>
  <view class="app">
    <router-view v-if="!isLoading" />
    <u-loading-page v-if="isLoading" color="#409eff"></u-loading-page>
  </view>
</template>

<script>
import { defineComponent, ref, onMounted } from 'vue';
import { useUserStore } from './store/user.js';
import { getToken } from './utils/storage.js';

export default defineComponent({
  name: 'App',
  setup() {
    const isLoading = ref(true);
    
    // 初始化应用
    const initApp = async () => {
      try {
        // 检查登录状态
        const token = getToken();
        if (token) {
          const userStore = useUserStore();
          await userStore.getUserInfo();
        }
        
        // 初始化其他配置
        await initConfig();
        
      } catch (error) {
        console.error('应用初始化失败:', error);
      } finally {
        // 完成加载
        setTimeout(() => {
          isLoading.value = false;
        }, 500); // 给用户一个短暂的加载体验
      }
    };
    
    // 初始化配置
    const initConfig = async () => {
      // 可以在这里加载一些全局配置
      // 例如：主题设置、语言设置等
    };
    
    onMounted(() => {
      initApp();
    });
    
    return {
      isLoading
    };
  }
});
</script>

<style lang="scss">
/*每个页面公共css */
/* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
@import "@/uni_modules/uview-plus/index.scss";

/* 全局样式重置 */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html, body {
  width: 100%;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'PingFang SC', 'Helvetica Neue', Helvetica, Arial, 'Microsoft YaHei', sans-serif;
  font-size: 16px;
  line-height: 1.5;
  color: #333333;
  background-color: #f5f5f5;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.app {
  width: 100%;
  height: 100%;
  position: relative;
}

/* 全局链接样式 */
a {
  color: #409eff;
  text-decoration: none;
}

a:hover {
  color: #66b1ff;
}

/* 全局按钮样式重置 */
button {
  border: none;
  outline: none;
  background: none;
  font-family: inherit;
  cursor: pointer;
}

/* 全局输入框样式重置 */
input, textarea {
  font-family: inherit;
  border: none;
  outline: none;
  background: none;
}

/* 全局滚动条样式 */
::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}

::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 3px;
}

::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}

/* 自定义主题颜色变量 */
:root {
  --primary-color: #409eff;
  --success-color: #67c23a;
  --warning-color: #e6a23c;
  --danger-color: #f56c6c;
  --info-color: #909399;
  --text-primary: #303133;
  --text-regular: #606266;
  --text-secondary: #909399;
  --text-placeholder: #c0c4cc;
  --border-color: #ebeef5;
  --background-color: #f5f7fa;
}

/* 深色模式支持 */
.dark-theme {
  --primary-color: #66b1ff;
  --success-color: #85ce61;
  --warning-color: #ebb563;
  --danger-color: #f78989;
  --info-color: #909399;
  --text-primary: #ffffff;
  --text-regular: #cccccc;
  --text-secondary: #999999;
  --text-placeholder: #666666;
  --border-color: #434343;
  --background-color: #1a1a1a;
  
  background-color: #1a1a1a;
  color: #ffffff;
}

/* 工具类 */
.text-center {
  text-align: center;
}

.text-right {
  text-align: right;
}

.flex {
  display: flex;
}

.flex-center {
  display: flex;
  align-items: center;
  justify-content: center;
}

.flex-between {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.flex-column {
  display: flex;
  flex-direction: column;
}

/* 动画效果 */
@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes slideUp {
  from {
    transform: translateY(20px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

/* 响应式断点 */
@media screen and (max-width: 768px) {
  html {
    font-size: 14px;
  }
}

@media screen and (max-width: 375px) {
  html {
    font-size: 13px;
  }
}

@media screen and (max-width: 320px) {
  html {
    font-size: 12px;
  }
}

/* 适配刘海屏和底部安全区 */
.safe-area-top {
  padding-top: env(safe-area-inset-top);
}

.safe-area-bottom {
  padding-bottom: env(safe-area-inset-bottom);
}

/* 禁用选择文本 */
.no-select {
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

/* 加载动画 */
.loading {
  animation: rotate 1s linear infinite;
}

@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
</style>
